﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="Base.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.Sql
{
    using System;
    using System.Data.Common;

    using Dhgms.DataManager.Model.Helper.Database;
    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Base class for an SQL task
    /// </summary>
    /// <typeparam name="TTaskResultClass">
    /// The Task Result Class used to return information about the task run.
    /// </typeparam>
    /// <typeparam name="TDatabaseHelperClass">
    /// The DB Helper Class used for commands
    /// </typeparam>
    /// <typeparam name="TParameterClass">
    /// The .NET DB Parameter Type
    /// </typeparam>
    public abstract class Base<TTaskResultClass, TDatabaseHelperClass, TParameterClass> : Task.Base<TTaskResultClass>
        where TTaskResultClass : Base, new()
        where TDatabaseHelperClass : DatabaseHelperBase<TParameterClass>, new()
        where TParameterClass : DbParameter
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Base{TTaskResultClass,TDatabaseHelperClass,TParameterClass}"/> class. 
        /// </summary>
        /// <param name="taskName">
        /// The name of the task
        /// </param>
        /// <param name="connectionString">
        /// Database Connection String
        /// </param>
        protected Base(string taskName, string connectionString)
            : base(taskName)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException("connectionString");
            }

            this.ConnectionString = connectionString;
            this.DbHelper = new TDatabaseHelperClass();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the database connection string
        /// </summary>
        protected string ConnectionString { get; set; }

        /// <summary>
        /// Gets the database Helper
        /// </summary>
        protected TDatabaseHelperClass DbHelper { get; private set; }

        #endregion

        #region Methods

        /// <summary>
        /// Gets a list of parameters for the SQL command
        /// </summary>
        /// <returns>
        /// list of parameters for the SQL command
        /// </returns>
        protected abstract TParameterClass[] GetParameters();

        /// <summary>
        /// Gets the SQL to execute
        /// </summary>
        /// <returns>
        /// the SQL to execute
        /// </returns>
        protected abstract string GetSql();

        #endregion
    }
}